/*
* 获取当前的地理位置、速度
* */

const amapGetLocation = (amap) => {
    return new Promise((resolve, reject) => {
        amap.getRegeo({
            async success(res) {
                let location = res[0]
                resolve(location)
            }, fail(e) {
                reject(e)
            }
        })
    })
}
//支付接口
const requestPayment = (payData) => {
    return new Promise((resolve, reject) => {
        wx.requestPayment({
            timeStamp: payData.timeStamp,
            nonceStr: payData.nonceStr,
            package: payData.package,
            signType: 'MD5',
            paySign: payData.paySign,
            success(res) {
                resolve(res)
            },
            fail(err) {
                reject(err)
            }
        })
    })
}
const getLocation = (type) => {
    return new Promise((resolve, reject) => {
        wx.getLocation({
            type: type||'gcj02',
            success: function (res) {
                resolve(res)
            },
            async fail(e) {
                let setting = await getSetting()
                if (!setting.authSetting['scope.userLocation']) {
                    wx.showModal({
                        title: '定位失败',
                        content: '未获取到你的地理位置，暂时无法为您提供服务，请检查是否已关闭定位权限，或请重新打开定位功能',
                        success(res) {
                            if (res.confirm) {
                                wx.openSetting({
                                    success(res) {
                                    }
                                })
                            } else if (res.cancel) {

                            }
                        }
                    })
                }
            }
        })
    })
}

/**
 * 获取用户的当前设置
 *  scope.userInfo            wx.getUserInfo  用户信息
 * scope.userLocation        wx.getLocation, wx.chooseLocation, wx.openLocation  地理位置
 * scope.address              wx.chooseAddress  通讯地址
 * scope.invoiceTitle        wx.chooseInvoiceTitle  发票抬头
 * scope.werun                wx.getWeRunData  微信运动步数
 * scope.record              wx.startRecord  录音功能
 * scope.writePhotosAlbum    wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum  保存到相册
 * scope.camera              <camera />  摄像头
 **/
const getSetting = () => {
    return new Promise((resolve, reject) => {
        wx.getSetting({
            success(res) {
                console.log(1111111111)
                resolve(res)
            },
            fail(e) {
                reject(e)
            }
        })
    })
}
const getCenterLocation = (map) => {
    return new Promise(resolve => {
        map.getCenterLocation({
            type: 'gcj02 ',
            async success(res) {
                resolve(res)
            }
        })
    })
}

// 网络链接检测

const getNetworkType = function () {
    return new Promise((resolve, reject) => {
        wx.getNetworkType({
            success(res) {
                resolve(res)
            },
            fail(e) {
                reject(e)
            }
        })
    })
}

// 写文件
const writeFiles = function (filePath, data) {
    return new Promise((resolve, reject) => {
        let fs = wx.getFileSystemManager()
        fs.writeFile({
            filePath: filePath,
            data: data,
            encoding: 'utf8',
            success(e) {
                resolve('success')
            },
            fail(e) {
                reject('fail')
            }
        })
    })
}
// 读文件
const readFiles = function (filePath) {
    return new Promise((resolve, reject) => {
        let fs = wx.getFileSystemManager()
        fs.readFile({
            filePath: filePath,
            encoding: 'utf8',
            success(res) {
                resolve(res)
            },
            fail(e) {
                reject(e)
            }
        })
    })
}
// 删除文件

const unlink = function (filePath) {
    return new Promise((resolve, reject) => {
        let fs = wx.getFileSystemManager()
        fs.unlink({
            filePath: filePath,
            success(e) {
                resolve('success')
            },
            fail(e) {
                resolve('e')
            }
        })
    })
}

// 图片压缩
const compressImage = function (imgsrc) {
    return new Promise((resolve, reject) => {
        if (wx.compressImage) {
            wx.compressImage({
                src: imgsrc,
                quality: 40,
                success(res) {
                    resolve(res)
                },
                fail(e) {
                    reject(e)
                }
            })
        } else {
            // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
            reject(false)
        }
    })
}
// 小程序登录获取code
const wxLogin = () => {
    return new Promise((resolve, reject) => {
        wx.login({
            success(res) {
                resolve(res)
            },
            fail(e) {
                reject(e)
            }
        })
    })
}
// 获取用户信息
const getUserInfo = () => {
    return new Promise((resolve, reject) => {
        wx.getUserInfo({
            success(userInfo) {
                resolve(userInfo)
            },
            fail(e){
                reject(e)
            }
        })
    })
}

//  获取图片信息
const getImageInfo = (url)=>{
    return new Promise((resolve, reject) => {
        wx.getImageInfo({
            src:url,
            success(userInfo) {
                resolve(userInfo)
            },
            fail(e){
                reject(e)
            }
        })
    })
}
// 获取地图移动中心点
//选择地图位置

const chooseLocation = (latitude,longitude)=>{
    return new Promise((resolve, reject) => {
        wx.chooseLocation({
            latitude,
            longitude,
            success(res) {
                resolve(res)
            },
            fail(e){
                reject(e)
            }
        })
    })
}
module.exports = {
    getLocation,
    getSetting,
    getCenterLocation,
    getNetworkType,
    writeFiles,
    readFiles,
    unlink,
    compressImage,
    wxLogin,
    getUserInfo,
    amapGetLocation,
    requestPayment,
    getImageInfo,
    chooseLocation
}
